package org.easy.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.easy.common.constant.TipConstant;
import org.easy.common.domain.SysDictDO;
import org.easy.common.domain.SysDictDataDO;
import org.easy.common.restinfo.RestInfo;
import org.easy.common.vo.SysDictVO;
import org.easy.module.mapper.SysDictDataMapper;
import org.easy.module.mapper.SysDictMapper;
import org.easy.module.service.SysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: SysDictServiceImpl
 * @Description: 数据字典service实现类
 * @Author zhouzhixin
 * @Date 2021/10/9
 * @Version 1.0
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDictDO> implements SysDictService {

    SysDictMapper sysDictMapper;

    SysDictDataMapper sysDictDataMapper;

    @Autowired
    public void init(SysDictMapper sysDictMapper, SysDictDataMapper sysDictDataMapper) {
        this.sysDictMapper = sysDictMapper;
        this.sysDictDataMapper = sysDictDataMapper;
    }

    @Override
    public List<SysDictVO> queryList() {
        List<SysDictVO> dictVOList = sysDictMapper.queryList();
        // 非空判断，为空则返回空集合
        if (CollectionUtils.isEmpty(dictVOList)) {
            dictVOList = new ArrayList<>();
        }
        return dictVOList;
    }

    @Override
    public RestInfo<SysDictVO> queryById(String id) {
        SysDictDO sysDictDO = sysDictMapper.selectById(id);
        SysDictVO sysDictVO = new SysDictVO();
        BeanUtils.copyProperties(sysDictDO, sysDictVO);
        return RestInfo.ok(sysDictVO);
    }

    @Override
    public void add(SysDictVO sysDictVO, Model model) {
        SysDictDO sysDictDO = new SysDictDO();
        BeanUtils.copyProperties(sysDictVO, sysDictDO);
        String checkRes = checkDict(sysDictDO);
        if (StringUtils.isNotBlank(checkRes)) {
            return;
        }
        boolean saveRes = save(sysDictDO);
        if (saveRes) {
            model.addAttribute("msg", "添加成功");
            model.addAttribute("code", 6);
        } else {
            model.addAttribute("msg", "添加失败");
            model.addAttribute("code", 5);
        }
    }

    @Override
    public RestInfo<Void> update(SysDictVO sysDictVO) {
        SysDictDO sysDictDO = new SysDictDO();
        BeanUtils.copyProperties(sysDictVO, sysDictDO);
        boolean updateRes = updateById(sysDictDO);
        if (updateRes) {
            return RestInfo.ok().build();
        }
        return RestInfo.fail().build();
    }

    @Transactional
    @Override
    public RestInfo<Void> delete(String id) {
        // 先删主表再删从表
        int dictDelFlag = sysDictMapper.deleteById(id);
        sysDictDataMapper.delete(new QueryWrapper<SysDictDataDO>().lambda().eq(SysDictDataDO::getDictId, id));
        if (dictDelFlag > 0) {
            return RestInfo.ok().build();
        }
        return RestInfo.fail().build();
    }

    private String checkDict(SysDictDO sysDictDO) {
        List<SysDictDO> dictDOList;
        dictDOList = sysDictMapper.selectList(new QueryWrapper<SysDictDO>().lambda()
                .eq(SysDictDO::getDictName, sysDictDO.getDictName()));
        if (!CollectionUtils.isEmpty(dictDOList)) {
            return TipConstant.CHECK_DICT_EXIST_DICT_NAME;
        }
        dictDOList = sysDictMapper.selectList(new QueryWrapper<SysDictDO>().lambda()
                .eq(SysDictDO::getDictCode, sysDictDO.getDictCode()));
        if (!CollectionUtils.isEmpty(dictDOList)) {
            return TipConstant.CHECK_DICT_EXIST_DICT_CODE;
        }
        return null;
    }
}
